package com.example.campus_activity_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.domain.Class;
import com.example.campus_activity_backend.domain.SysUser;
import com.example.campus_activity_backend.domain.Teacher;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.ClassService;
import com.example.campus_activity_backend.mapper.ClassMapper;
import com.example.campus_activity_backend.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Hongxia
* @description 针对表【t_manager_class】的数据库操作Service实现
* @createDate 2023-11-17 14:32:36
*/
@Service
public class ClassServiceImpl extends ServiceImpl<ClassMapper, Class>
    implements ClassService {
    @Autowired
    private TeacherService teacherService;

    @Override
    public long addClass(Class classes) {
        //校验参数
        if(classes == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }
        String classesName = classes.getName();
        Integer activityId = classes.getActivityId();
        if(classesName == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教学班名称为空");
        }else if(activityId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"关联活动id为空");
        }
        classes.setCreatedTime(new Date());
        //添加到数据库
        boolean saveResult = this.save(classes);
        if(!saveResult){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"教学班创建失败");
        }

        return classes.getId();
    }

    @Override
    public boolean addTeacherForClass(Integer teacherId, Integer classId) {
        //参数校验
        if(teacherId == null ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教师id为空");
        }
        if(classId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"教学班id为空");
        }
        //校验id是否存在
        Class classes = this.getById(classId);
        if(classes == null){
            throw new BusinessException(ErrorCode.NO_FOUND_ERROR,"教学班不存在");
        }
        Teacher teacher = teacherService.getById(teacherId);
        if(teacher == null){
            throw new BusinessException(ErrorCode.NO_FOUND_ERROR,"教师不存在");
        }
        //校验教学班已配置教师数量
        QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", 1)
                .isNotNull("teacher_id");
        int count = this.count(queryWrapper);
        if(count > 2){
            throw new BusinessException("教学班最多可配两名教师",400,"教学班最多可配两名教师");
        }
        //添加到数据库
        if(count == 0){
            this.update().setSql("teacher_id = " + teacherId).eq("id",classId).update();
        }
        return true;
    }
}




